<?php
include_once './util/db/Conn.php';
include_once './util/ArrayList.php';

include_once './util/db/model/Product.php';
include_once './util/db/model/ProdType.php';
include_once './util/db/model/ProdSubType.php';
include_once './util/db/model/SupplierBrand.php';
include_once './util/db/model/ProductSearchPara.php';
include_once './util/db/model/ProdAttr.php';

class ProductDBUtil {
	protected $dbConn;
	private $tabName = "product";
//	private $joinSql = "SELECT a.p_id, a.prod_no, a.p_name, DATE_FORMAT(a.add_date,'%Y/%m/%d') 
//	add_date, a.sort, a.sb_id, a.price, a.sell_price, a.sell_mode, a.pre_year, a.pre_month, 
//	a.pre_day, a.ship_month, a.ship_day, a.title, a.pic, a.type, prod_txt , pst_id , b.type prodType_TYPE,
//	 sub_type, pt_id , cm_id , logo_name , logo_name_en, d.PIC SB_PIC, c.TYPE TYPE_DESC
//	 , e.PA_ID, e.COUNT prodAttr_COUNT, e.ATTR prodAttr_ATTR 
//	 FROM product a JOIN prod_sub_type b ON a.SORT=b.PST_ID JOIN prod_type c ON b.TYPE=c.PT_ID JOIN supplier_brand d ON a.SB_ID=d.SB_ID JOIN prod_attr e ON a.P_ID=e.P_ID ";
	
	private $joinSql = "SELECT a.p_id, a.prod_no, a.p_name, DATE_FORMAT(a.add_date,'%Y/%m/%d') 
	add_date, a.sort, a.sb_id, a.price, a.sell_price, a.sell_mode, a.pre_year, a.pre_month, 
	a.pre_day, a.ship_month, a.ship_day, a.title, a.pic,  prod_txt , pst_id , b.type prodType_TYPE,
	 sub_type, pt_id , cm_id , logo_name , logo_name_en, d.PIC SB_PIC, c.TYPE TYPE_DESC
	
	 FROM product a JOIN prod_sub_type b ON a.SORT=b.PST_ID JOIN prod_type c ON b.TYPE=c.PT_ID 
	 JOIN supplier_brand d ON a.SB_ID=d.SB_ID  ";
	function __construct() {
		$this->dbConn = new DBConn ();
		@header ( "Content-Type:text/html; charset=utf-8" );
	}
	
	public function open() {
		$this->dbConn->open ();
	
	}
	
	public function close() {
		$this->dbConn->close ();
	}
	
	public function queryAll() {
		$this->open ();
		$arrayList = new ArrayList ();
		$sql = $this->joinSql;
		$result = mysql_query ( $sql );
		while ( $row = @mysql_fetch_array ( $result ) ) {
			$arrayList->add ( $this->fill ( $row ) );
		}
		$this->close ();
		return $arrayList;
	}
	
	/**
	 * Enter description here ...
	 */
	private function fill($row) {
		$product = new Product ();
		$product->setP_id ( $row ['p_id'] );
		$product->setProd_no ( $row ['prod_no'] );
		$product->setP_name ( $row ['p_name'] );
		$product->setAdd_date ( $row ['add_date'] );
		$product->setSort ( $row ['sort'] );
		$product->setSb_id ( $row ['sb_id'] );
		$product->setPrice ( $row ['price'] );
		$product->setSell_price ( $row ['sell_price'] );
		$product->setSell_mode ( $row ['sell_mode'] );
		$product->setPre_year ( $row ['pre_year'] );
		$product->setPre_month ( $row ['pre_month'] );
		$product->setPre_day ( $row ['pre_day'] );
		$product->setShip_month ( $row ['ship_month'] );
		$product->setShip_day ( $row ['ship_day'] );
		$product->setTitle ( $row ['title'] );
		$product->setPic ( $row ['pic'] );
		$product->setProd_txt ( $row ['prod_txt'] );
		//$product->setType ( $row ['type'] );
		$prodType = new ProdType ();
		$prodType->setPt_id ( $row ['pt_id'] );
		$prodType->setType ( $row ['TYPE_DESC'] );
		$product->setProdType ( $prodType );
		$prodSubType = new ProdSubType ();
		$prodSubType->setPst_id ( $row ['pst_id'] );
		$prodSubType->setType ( $row ['prodType_TYPE'] );
		$prodSubType->setSub_type ( $row ['sub_type'] );
		$product->setProdSubType ( $prodSubType );
		$supplierBrand = new SupplierBrand ();
		$supplierBrand->setSb_id ( $row ['sb_id'] );
		$supplierBrand->setCm_id ( $row ['cm_id'] );
		$supplierBrand->setLogo_name ( $row ['logo_name'] );
		$supplierBrand->setLogo_name_en ( $row ['logo_name_en'] );
		$supplierBrand->setPic ( $row ['SB_PIC'] );
		$product->setSupplierBrand ( $supplierBrand );
		
		$prodAttr = new ProdAttr ();
		$prodAttr->setPa_id ( $row ['PA_ID'] );
		$prodAttr->setP_id ( $row ['p_id'] );
		$prodAttr->setCount ( $row ['prodAttr_COUNT'] );
		$prodAttr->setAttr ( $row ['prodAttr_ATTR'] );
		$product->setProdAttr ( $prodAttr );
		return $product;
	}
	
	//	private function genInsertIntoSql($orderTab) {
	//		$insertIntoSql = "insert into order_tab(goOrback,takeDownAddress,takeDownRegion,takeUpAddress,takeUpOrder,takeUpRegion,orderUserId,orderDate,orderTime) values(
	//		$orderTab->getGoOrBack(),'$orderTab->getTakeDownAddress()','$orderTab->getTakeDownRegion()','$orderTab->getTakeUpAddress()',$orderTab->getTakeUpOrder(),'$orderTab->getTakeUpRegion()',$orderTaB->getOrderUserId(),DATE_FORMAT('$orderTab->getOrderDate()' , '%Y%m%d'),$orderTab->getOrderTime())";
	//		return $insertIntoSql;
	//	}
	

	public function queryAllByPara($para) {
		$this->open ();
		$arrayList = new ArrayList ();
		$sql = $this->joinSql . " where 1=1 and cm_id=".$_SESSION['companyMember']->{'cm_id'};
		//echo $sql.'<br/>';
		if ( null != $para->getP_name() && $para->getP_name() != "") {
			$sql = $sql . " and a.p_name like '%" . $para->getP_name () . "%' ";
		}
		if ( null != $para->getAdd_date () && $para->getAdd_date () != "") {
			$sql = $sql . " and a.add_date=DATE_FORMAT('" . $para->getAdd_date () . "', '%Y%m%d') ";
		}
		if ( null != $para->getSell_mode () && $para->getSell_mode () != "") {
			if (count ( $para->getSell_mode () ) > 0) {
				$sql = $sql . " and ( ";
			}
			for($i = 0; $i < count ( $para->getSell_mode () ); $i ++) {
				$value = $para->getSell_mode ();
				if ($i == 0) {
					$sql = $sql . " a.sell_mode=$value[$i] ";
				} else {
					$sql = $sql . " or sell_mode=$value[$i] ";
				}
			}
			if (count ( $para->getSell_mode () ) > 0) {
				$sql = $sql . " ) ";
			}
		}
		if ( null != $para->getSb_id () && $para->getSb_id () != "") {
			$sql = $sql . " and a.sb_id =" . $para->getSb_id () . " ";
		}
		
		if ( null != $para->getSort () && $para->getSort () != "") {
			$sql = $sql . " and a.sort =" . $para->getSort () . " ";
		}
		if ( null != $para->getType() && $para->getType() != "") {
			$sql = $sql . " and a.type=" . $para->getType () . " ";
		}
		if ( null != $para->getP_id() && $para->getP_id () != "") {
			$sql = $sql . " and a.p_id=" . $para->getP_id () . " ";
		}
		//echo $sql;
		$result = mysql_query ( $sql );
		while ( $row = @mysql_fetch_array ( $result ) ) {
			@$arrayList->add ( $this->fill ( $row ) );
		}
		$this->close ();
		return $arrayList;
	}
	
	public function queryByPid($pid) {
		$this->open ();
		$sql = $this->joinSql . " where a.p_id=$pid";
		$result = mysql_query ( $sql );
		while ( $row = @mysql_fetch_assoc ( $result ) ) {
			@$product = $this->fill ( $row );
		}
		$this->close ();
		return @$product;
	}
}
?>